package project.tool4nat.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Config {

    public static String SERVER_HOST;

    public static int SERVER_PORT;

    public static String CONTENT_SERVER_HOST;

    public static int CONTENT_SERVER_PORT;

    public static int CALLER_PORT;

    static {
        String configName = System.getProperty("config.name");
        if (configName == null || configName.isEmpty()) {
            configName = "config.properties";
        }
        String configPath = System.getProperty("user.dir");
        InputStream in = null;
        try {
            File file = new File(configPath, configName);
            if (file.exists() && file.isFile()) {
                in = new FileInputStream(file);
            } else {
                in = Config.class.getClassLoader().getResourceAsStream("config.properties");
            }
            Properties properties = new Properties();
            properties.load(in);
            SERVER_HOST = properties.getProperty("server.host", "127.0.0.1");
            SERVER_PORT = Integer.parseInt(properties.getProperty("server.port", "7777"));
            CONTENT_SERVER_HOST = properties.getProperty("content.host", "127.0.0.1");
            CONTENT_SERVER_PORT = Integer.parseInt(properties.getProperty("content.port", "8080"));
            CALLER_PORT = Integer.parseInt(properties.getProperty("caller.port", "8888"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
